package com.zbkj.front.controller.publicly.device;

import com.zbkj.common.config.AliMqttConfig;
import com.zbkj.common.request.device.DeviceRequest;
import com.zbkj.common.response.device.DeviceResponse;
import com.zbkj.common.model.device.MqttConfig;
import com.zbkj.service.service.device.DeviceService;
import com.zbkj.service.service.device.MqttConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 设备公开接口控制器
 * +----------------------------------------------------------------------
 * | 作者: xiaolic
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/public/device")
@Api(tags = "设备-公开接口")
public class DevicePublicController {

    @Autowired
    private DeviceService deviceService;
    
    @Autowired
    private MqttConfigService mqttConfigService;

    @Autowired
    private AliMqttConfig aliMqttConfig;

    /**
     * 设备注册
     * 简化后的注册接口，只需提供MAC地址和IMEI信息
     */
    @ApiOperation(value = "设备注册", notes = "设备注册接口，只需提供MAC地址和IMEI信息")
    @PostMapping("/register")
    public DeviceResponse registerDevice(@RequestBody @Validated DeviceRequest request) {
        try {
            log.info("设备注册请求: {}", request);
            
            // 直接通过字段名获取参数
            String macAddress = request.getMacAddress();
            String imei = request.getImei();
            
            // 注册设备并获取设备SN编号
            String deviceSN = deviceService.registerDeviceByFields(macAddress, imei);
            
            log.info("设备注册成功，设备SN编号: {}", deviceSN);
            return DeviceResponse.success(deviceSN);
        } catch (Exception e) {
            log.error("设备注册失败", e);
            return DeviceResponse.fail("注册失败: " + e.getMessage());
        }
    }

    /**
     * 获取MQTT信息
     * 根据设备SN编号或MAC地址或设备IMEI获取对应的MQTT配置信息
     */
    @ApiOperation(value = "获取MQTT信息", notes = "根据设备SN编号或MAC地址或设备IMEI获取对应的MQTT配置信息")
    @PostMapping("/getMQTT")
    public DeviceResponse getMqttInfo(@RequestBody @Validated DeviceRequest request) {
        try {
            log.info("获取MQTT信息请求: {}", request);

            MqttConfig mqttConfig = mqttConfigService.getConfigByDevice(request);

            Map<String, String> simpleMqttConfig = new HashMap<>();

            simpleMqttConfig.put("mqttServerUrl",  mqttConfig.getMqttServerUrl());
            simpleMqttConfig.put("clientID", mqttConfig.getClientId());
            // simpleMqttConfig.put("clientUserName", mqttConfig.getAccessKey());
            // simpleMqttConfig.put("clientPWD", mqttConfig.getSecretKey());
            simpleMqttConfig.put("deviceSN", mqttConfig.getDeviceSN());
            simpleMqttConfig.put("clientUserName", mqttConfig.getConnUsername());
            simpleMqttConfig.put("clientPWD", mqttConfig.getConnPassword());
            
            log.info("获取MQTT信息成功");
            return DeviceResponse.success(simpleMqttConfig);
        } catch (Exception e) {
            log.error("获取MQTT信息失败", e);
            return DeviceResponse.fail("获取MQTT信息失败: " + e.getMessage());
        }
    }

}
